package com.eascs.web.o2o.web.reward.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.context.RequestContext;
import com.eascs.web.o2o.web.reward.entity.LoanDetailForm;
import com.eascs.web.o2o.web.reward.service.ValidLoanService;

@RestController
@RequestMapping(value="/restValidLoan")
public class RewardValidLoanRestController extends BaseRestController{
	
	@Autowired
	ValidLoanService validLoanService;

	/**
	 * 有效投放总额报表
	 * @param entity
	 * @return
	 */
	@RequestMapping(value="/validLoanList")
	private PaginationRestResult loanDetailList(LoanDetailForm entity) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getCheckMonth()==null?date:entity.getCheckMonth();
		entity.setCheckMonth(month);
		String o2oUrl = "/restValidLoan/validLoanList";
		if(entity != null) {
			String checkMonth = entity.getCheckMonth();
			String provinceName = entity.getProvinceName();
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "checkMonth", checkMonth);
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "provinceName", provinceName);
			o2oUrl = o2oUrl + "&showPage=";
		} 
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = validLoanService.reportForValidLoan(entity, "search");
		String[] titles = {"考核月份", "省区", "工号", "姓名", "考核类型", "当期新增投放（万元）", "当期续贷投放金额(万元)", "当期逾期本息金额(万元)", "当期回收贷款本息金额(万元)", "新增投放系数", "续贷投放系数", "有效投放总额(万元)"};
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "showPage", ""));
		paginationInfo.setFirstRenderColumnIndex(0);
		paginationInfo.setFormats(new Format[] {null, null, null, null, null, new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), null, null, new DecimalFormat(",##0.00")});
		paginationInfo.setRendTo("#o2o_result");
		ViewContext.put("vo", entity);
		ViewContext.put("o2oUrl", o2oUrl);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
	
}
